//
//  YCTitleContentViewController.m
//  TitleContent
//
//  Created by 唐可雄 on 2019/7/14.
//  Copyright © 2019 swustyc. All rights reserved.
//

#import "YCTitleContentViewController.h"

#import "YCTitleSegmentView.h"
#import "YCTitleCollectionView.h"
#import "YCContentPageView.h"

@interface YCTitleContentViewController ()<YCTitleSegmentViewDataSource, YCTitleViewDelegate, YCTitleCollectionViewDataSource, YCContentViewDataSource, YCContentViewDelegate>

@property (nonatomic, strong) YCTitleView *titleView;
@property (nonatomic, strong) YCContentView *contentView;

@end

@implementation YCTitleContentViewController

#pragma mark - UIViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self setSelectedIndex:self.selectedIndex];
}

#pragma mark -Display

- (void)displayInParentViewController:(UIViewController *)parentVC {
    [parentVC addChildViewController:self];
    [parentVC.view addSubview:self.view];
    [parentVC.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:parentVC.view.safeAreaLayoutGuide attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [parentVC.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:parentVC.view.safeAreaLayoutGuide attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
    [parentVC.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:parentVC.view.safeAreaLayoutGuide attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
    [parentVC.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:parentVC.view.safeAreaLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
}

#pragma mark - Parent-Child View Controller

- (void)willMoveToParentViewController:(UIViewController *)parent {
    [super willMoveToParentViewController:parent];
    if (parent) {
        [self configView];
    }
}

#pragma mark - View Config

/**
 配置视图
 */
- (void)configView {
    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    
    self.titleView = [self createTitleView];
    self.contentView = [self createContentView];
    switch (self.config.titleViewPos) {
        case YCTitleViewPosNormal:
        {
            self.titleView.translatesAutoresizingMaskIntoConstraints = NO;
            [self.view addSubview:self.titleView];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
            [self.titleView addConstraint:[NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:self.config.titleViewHeight]];
            
            [self.view addSubview:self.contentView];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.titleView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
        }
            break;
        case YCTitleViewPosNavi:
        {
            self.parentViewController.navigationItem.titleView = self.titleView;
            
            [self.view addSubview:self.contentView];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
        }
            break;
        default:
            break;
    }
}

/**
 创建内容

 @return 内容
 */
- (YCContentPageView *)createContentView {
    YCContentPageView *contentView = [[YCContentPageView alloc] init];
    contentView.dataSource = self;
    contentView.delegate = self;
    return contentView;
}

/**
 构造标题栏

 @return 标题栏
 */
- (YCTitleView *)createTitleView {
    switch (self.config.titleViewStyle) {
        case YCTitleViewStyleSegment:
        {
            YCTitleSegmentView *titleView = [[YCTitleSegmentView alloc] init];
            titleView.backgroundColor = self.config.titleViewBackgroundColor;
            titleView.config = self.config;
            titleView.dataSource = self;
            titleView.delegate = self;
            return titleView;
        }
            break;
        case YCTitleViewStyleCollection:
        {
            YCTitleCollectionView *titleView = [[YCTitleCollectionView alloc] init];
            titleView.backgroundColor = self.config.titleViewBackgroundColor;
            titleView.config = self.config;
            titleView.dataSource = self;
            titleView.delegate = self;
            return titleView;
        }
            break;
        default:
            break;
    }
    return nil;
}

#pragma mark - Select

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    
    [self.titleView setSelectedIndex:selectedIndex];
    [self.contentView setSelectedIndex:selectedIndex];
}

#pragma mark - Load

- (void)reloadData {
    [self.titleView reloadData];
    [self.contentView reloadData];
    [self setSelectedIndex:0];
}

#pragma mark - YCTitleViewDataSource

- (NSUInteger)numberOfTitlesInTitleView:(YCTitleView *)titleView {
    return [self.dataSource numberOfTitlesInTitleContentViewController:self];
}

#pragma mark - YCTitleSegmentViewDataSource

- (NSString *)titleSegmentView:(YCTitleSegmentView *)titleView titleAtIndex:(NSUInteger)index {
    return [self.dataSource titleContentViewController:self textTitleForIndex:index];
}

#pragma mark - YCTitleViewDelegate

- (void)titleView:(YCTitleView *)titleView didSelectAtIndex:(NSUInteger)index {
    _selectedIndex = index;
    [self.contentView setSelectedIndex:_selectedIndex];
}

#pragma mark - YCTitleCollectionViewDataSource

- (NSString *)titleCollectionView:(YCTitleCollectionView *)titleView titleAtIndex:(NSUInteger)index {
    return [self.dataSource titleContentViewController:self textTitleForIndex:index];
}

- (UIButton *)rightButtonInTitleCollectionView:(YCTitleCollectionView *)titleView {
    if ([self.dataSource respondsToSelector:@selector(rightButtonInTitleContentViewController:)]) {
        return [self.dataSource rightButtonInTitleContentViewController:self];
    }
    return nil;
}

#pragma mark - YCContentViewDataSource

- (NSUInteger)numberOfPagesInContentView:(YCContentView *)contentView {
    return [self.dataSource numberOfTitlesInTitleContentViewController:self];
}

- (UIViewController *)contentView:(YCContentView *)titleView viewControllerAtIndex:(NSUInteger)index {
    return [self.dataSource titleContentViewController:self contentViewControllerForIndex:index];
}

#pragma mark - YCContentViewDelegate

- (void)contentView:(YCContentView *)contentView transitionToTargetIndex:(NSUInteger)index progress:(CGFloat)progress {
    if (self.config.titleViewStyle == YCTitleViewStyleCollection) {
        [((YCTitleCollectionView *)self.titleView) transitionToTargetIndex:index progress:progress];
    }
}

- (void)contentView:(YCContentView *)contentView didSelectAtIndex:(NSUInteger)index {
    _selectedIndex = index;
    [self.titleView setSelectedIndex:_selectedIndex];
}

@end
